package InterfazGrafica;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Util.SocketManager;

public class VentanaMaquina extends JFrame {


	private static final long serialVersionUID = 1L;
	private SocketManager sm;
	
	private int monedas5cent;
	private int monedas10cent;
	private int monedas20cent;
	private int monedas50cent;
	private int monedas1eur;
	private int monedas2eur;
	
	private JLabel credito;
	private double creditoActual;
	
	private JButton boton5cent;
	private JButton boton10cent;
	private JButton boton20cent;
	private JButton boton50cent;
	private JButton boton1eur;
	private JButton boton2eur;
	private JButton pagar1e;
	private JButton pagar18e;
	private JButton pagar065e;
	private JButton pagar21e;
	
	private JPanel panelBotones;
	private JPanel panelCredito;
	private JPanel panelIzquierdo;

	public VentanaMaquina(SocketManager socket) {
		sm = socket;
		creditoActual = 0;

		this.setSize(800,550);
		this.setResizable(false);
		this.setLocation((getToolkit().getScreenSize().width - this.getBounds().width) / 2,
				(getToolkit().getScreenSize().height - this.getBounds().height) / 2);

		credito = new JLabel("Credito: " + creditoActual + " �");
		
		boton5cent = new JButton("5 cents");
		boton10cent = new JButton("10 cents");
		boton20cent = new JButton("20 cents");
		boton50cent = new JButton("50 cents");
		boton1eur = new JButton("1 euro");
		boton2eur = new JButton("2 euros");
		pagar1e = new JButton("Pagar 1�");
		pagar18e = new JButton("Pagar 1,8�");
		pagar065e = new JButton("Pagar 0,65�");
		pagar21e = new JButton("Pagar 2,1�");
		
		panelBotones = new JPanel();
		panelBotones.setLayout(new GridLayout(3,2));
		panelBotones.add(boton5cent);
		panelBotones.add(boton10cent);
		panelBotones.add(boton20cent);
		panelBotones.add(boton50cent);
		panelBotones.add(boton1eur);
		panelBotones.add(boton2eur);
		
		panelCredito = new JPanel();
		panelCredito.add(credito);
		
		panelIzquierdo = new JPanel();
		panelIzquierdo.setLayout(new GridLayout(2,2));
		panelIzquierdo.add(pagar1e);
		panelIzquierdo.add(pagar18e);
		panelIzquierdo.add(pagar065e);
		panelIzquierdo.add(pagar21e);
		
		this.setLayout(new BorderLayout());
		this.getContentPane().add("Center", panelBotones);
		this.getContentPane().add("South", panelCredito);
		this.getContentPane().add("East", panelIzquierdo);
		
		boton5cent.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(5);
			}
		});

		boton10cent.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(10);
			}
		});

		boton20cent.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(20);
			}
		});

		boton50cent.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(50);
			}
		});

		boton1eur.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(100);
			}
		});

		boton2eur.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				actualizarCredito(200);
			}
		});
		
		pagar1e.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				pagarImporte(100);
			}
		});
		
		pagar18e.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				pagarImporte(180);
			}
		});
		
		pagar065e.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				pagarImporte(65);
			}
		});
		
		pagar21e.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				pagarImporte(210);
			}
		});
	}
	
	public void actualizarCredito (double x) {
		creditoActual = creditoActual + x;
		credito.setText("Credito: " + creditoActual / 100 + " �");
	}
	
	public void pagarImporte (double importe) {
		double devolver = creditoActual - importe;
		if (devolver < 0) {
			JOptionPane.showMessageDialog(null, "No hay suficiente credito");
		}
		else {
			int c5 = 0, c10 = 0, c20 = 0, c50 = 0, e1 = 0;
			int x = (int) devolver;
			int centenas = x / 100;
			int unidad = x % 100;
			int decenas = unidad / 10;
			unidad = x % 10;
			
			e1 = centenas;
			if (decenas >= 5) {
				c50 = 1;
				decenas = decenas - 5;
			}
			
			if (decenas == 4) {
				c20 = 2;
			}
			
			else if (decenas == 3) {
				c20 = 1;
				c10 = 1;
			}
			
			else if (decenas == 2) {
				c20 = 1;
			}
			
			else if (decenas == 1) {
				c10 = 1;
			}
			
			if (unidad == 5)
				c5 = 1;
			
			JOptionPane.showMessageDialog(null, "El importe a devolver es de: " + devolver / 100);
			JOptionPane.showMessageDialog(null, "En " + e1 + " monedas de 1�, " + c50 + " de 50, " + c20 + " de 20, " + c10 + 
					" de 10 y " + c5 + " de 5");
		}
	}
	
	public static void main (String[] args) {
		VentanaMaquina vm = new VentanaMaquina(null);
		vm.setVisible(true);
	}
}
